export function toCurrency(value: number, symbol: string = '¥'): string {
    const places = 2;
    const thousand = ",";
    const decimal = ".";
    let number = value + '',
        negative = value < 0 ? "-" : "",
        i = parseInt(number = Math.abs(+number || 0).toFixed(places), 10),
        j = 0
    j = (j = (i + "").length) > 3 ? j % 3 : 0;
    return symbol + negative + (j ? (i + "").substr(0, j) + thousand : "") + (i + "").substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousand) + (places ? decimal + Math.abs(value - i).toFixed(places).slice(2) : "");
};

export function fix(value: number): number {
    return Math.round(value * 1000) / 1000
}

export function ageFromDate(date: string, accuracy: number): number {
    //日期转化为number类型
    let year = new Date(date).getFullYear();
    let month = new Date(date).getMonth();
    let myDate = new Date();
    myDate.toLocaleDateString();//获取当前日期
    let nowYear = myDate.getFullYear();
    let nowMonth = myDate.getMonth();
    let dValue = 0;
    if (nowMonth >= month) {
        dValue = nowYear - year + (nowMonth - month) / 12;
    } else {
        dValue = nowYear - year + (month - nowMonth) / 12;
    }
    return accuracy !== 1 ? parseInt(dValue.toFixed(1)) == parseFloat(dValue.toFixed(1)) ? dValue : Math.floor(dValue) + 0.5 : Math.floor(dValue)
}

//是否可入场
export function availableDateStr(available: string): string {
    let msg = ""
    if (new Date(available) < new Date()) {
        msg = "随时"
    } else {
        let _available = new Date(available);
        let year = _available.getFullYear();
        let month = _available.getMonth() + 1;
        let day = _available.getDate();//获得日
        if (day >= 1 && day <= 10) {
            return `${month}月上旬`
        } else {
            if (day >= 11 && day <= 19) {
                return `${month}月中旬`
            } else {
                return `${month}月下旬`
            }
        }
    }
    return msg;
}

export function toShortDateStr(date: Date): string {
    return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`
}